const requsts = require("/util/requst");
const util = require("/util/util");
Page({
  data: {
    indicatorcolor:"#f5f5f5",
    indicatoractive:"#FF5000",
    tabBarActiveTextColor:"#FF5000",
    tabBarUnderlineColor:"FF5000",
    showBottom:false,
    disableScroll:false,
    isBtn:false,
    isBtns:false,
    subarr:[],
    activearr:null,
    banner:[],
    indicatorDots: true,
    autoplay: false,
    vertical: false,
    interval: 1000,
    circular: false,
    isswipeable:false,
    animated:false,
    lazyload:true,
    value:1,
    defaultsource:"https://gw.alipayobjects.com/zos/rmsportal/VBqNBOiGYkCjqocXjdUj.png",
    tabs: [
      {
        title: '商品详情',
        url:""
      },
      {
        title: '租用规则',
        url:""
      },
    ],
    activeTab: 0,
    productId:"",
    dataObj:{},
    authCode:"",
    canIUseAuthButton:false,
    id:"",
    buyCount:1,
    authCode:"",
    userId:"",
		rentConfigList:[],
		rentConfigdata:"",
		isrentConfig:false,
    iszuidi:0,
    iszuidiarr:[],
    isbz:0.00,
    isStock:0,
  },
  handleTabClick({ index }) {
    this.setData({
      activeTab: index,
    });
  },
  handleTabChange({ index }) {
    this.setData({
      activeTab: index,
    });
  },
  handlePlusClick() {
    my.alert({
      content: 'plus clicked',
    });
  },
  //开启提交订单
  onButtomBtnTap(){
    this.setData({
      showBottom: true,
    });
    var that=this
    my.getStorage({
      key: 'userId',
      success: function(res) {
        console.log(res)
        if(res.data==null){
            my.getAuthCode({
            scopes: 'auth_base',
            success: (res) => {
              that.setData({
                authCode:res.authCode,
                canIUseAuthButton:true,
              })
            },
          });
        }else{
            that.setData({
                canIUseAuthButton:false
            })
        }
      },
      fail: function(res){
      }
    });
  },
  //关闭提交订单
  onPopupClose() {
    this.setData({
      showBottom: false,
    });
  },
  onLoad(e){
      this.init(e)
      // my.setStorage({
      //   key: 'userId',
      //   data: {
      //     userId: '2088522144147466',
      //   },
      //   success: function() {
      //   }
      // });
  },
  whf(){
      this.setData({
        iszuidi:e.currentTarget.dataset.item.monthlyPrice,
        isbz:e.currentTarget.dataset.item.contingencyAmount
      })
			//this.data.activearr[e.target.dataset.index].isactive=!this.data.activearr[e.target.dataset.index].isactive
		var isactivess=null,arr=this.data.rentConfigList
    for(var i = 0;i<arr.length;i++){
			arr[i].isactive=false
      if(i==e.target.dataset.index){
          arr[i].isactive=!arr[i].isactive
      }
    }
    arr[0].isactive=true
		this.setData({
			rentConfigList:arr,
			rentConfigdata:this.data.rentConfigList[e.target.dataset.index].rentId
		})
  },
  init(e){
    this.setData({
          productId:e.productType,
        });
    var param={
      productId:this.data.productId
    }
    var that=this
       my.request({
        url: requsts.url+"/product/getProductDetailInfo",
        method: 'POST',
        headers: {'content-type': 'application/x-www-form-urlencoded'},
        data:param,
        dataType: 'json',
        success: function(res) {
          console.log(res)
          if(res.data.code!=0){
              util.toastfn(res.data.msg)
              return
          }
          that.setData({
              dataObj:res.data.data,
              activearr:res.data.data.rentTypeList,
							rentConfigList:res.data.data.rentConfigList,
              banner:res.data.data.slideshowList,
              id:res.data.data.id,
          })
					var arr=that.data.rentConfigList
					for(var i = 0;i<arr.length;i++){
						arr[i].isactive=false
					}
          arr[0].isactive=true
          
          that.setData({
            iszuidi:(res.data.data.rentConfigList)[0].monthlyPrice,
            isbz:(res.data.data.rentConfigList)[0].contingencyAmount
          })
					console.log(arr)
					that.setData({
						rentConfigList:arr
					})
          that.setData({
						rentConfigdata:(res.data.data.rentConfigList)[0].rentId
					})
        //   if(this.data.subarr.length){

        //   }
        //   	this.setData({
        //   isBtn:true,
        //   isBtns:true
        // })
          var tabs=that.data.tabs
          for(var i = 0;i<tabs.length;i++){
              if(i==0){
                  tabs[i].url=that.data.dataObj.detailDesc
              }else if(i==1){
                  tabs[i].url=that.data.dataObj.rentDetailUrl
              }
          }
          that.setData({
              tabs,
          })
        },
        fail: function(res) {
            util.toastfn(res.errorMessage)
        },
      });
  },
	onAuthError(){
			 this.setData({
      showBottom: false,
    });
	},
	activefn1(e){
      this.setData({
        iszuidi:e.currentTarget.dataset.item.monthlyPrice,
        isbz:e.currentTarget.dataset.item.contingencyAmount
      })
			//this.data.activearr[e.target.dataset.index].isactive=!this.data.activearr[e.target.dataset.index].isactive
		var isactivess=null,arr=this.data.rentConfigList
    for(var i = 0;i<arr.length;i++){
			arr[i].isactive=false
      if(i==e.target.dataset.index){
          arr[i].isactive=!arr[i].isactive
      }
    }
		this.setData({
			rentConfigList:arr,
			rentConfigdata:this.data.rentConfigList[e.target.dataset.index].rentId
		})
    console.log(arr)
		if(this.data.rentConfigdata!=""){
			if(this.data.subarr.length>=this.data.activearr.length){
					this.setData({
						isBtns:true,
						isBtn:true,
					})
			}
		}else{
			if(this.data.subarr.length<this.data.activearr.length){
					this.setData({
						isBtn:false,
						isBtns:false
					})
			}
		}
		console.log(arr)
		console.log(this.data.rentConfigdata)
	},
  activefn(e){
    //选中or不选中的操作
    this.data.activearr[e.target.dataset.index].isactive=!this.data.activearr[e.target.dataset.index].isactive
    var isactivess=null,arr=this.data.activearr
    for(let i = 0;i<arr;i++){
      if(i==e.target.dataset.index){
          isactivess=!this.data.activearr[i].isactive
          arr[i].isactive=!arr[i].isactive
      }
    }
    this.setData({
      activearr:arr
    })
    /********************************选中push数组，没选中切割*****************************************/ 
     var istrue=this.data.activearr[e.target.dataset.index].isactive;
     if(istrue){
       this.data.subarr.push(e.currentTarget.dataset.item.id)
     }else{
       this.removeByVal(this.data.subarr,e.currentTarget.dataset.item.id)
     }
		 console.log(this.data.rentConfigdata)
    if(this.data.subarr.length>=this.data.activearr.length){
			if(this.data.iszuidi){
					this.setData({
          isBtn:true,
          isBtns:true
        })
			}else{
				this.setData({
          isBtn:false,
          isBtns:false
        })
			}
    }else{
      this.setData({
          isBtn:false,
          isBtns:false
        })
    }
    console.log(this.data.subarr)
  },
  removeByVal(arrylist , val) {
    for(var i = 0; i < arrylist .length; i++) {
      if(arrylist [i] == val) {
        arrylist .splice(i, 1);
        break;
      }
    }
  },
  submitfn(){

    if(!this.data.isBtn){
        return
    }
    var that=this
    my.getStorage({
      key: 'userId',
      success: function(res) {
        console.log(res)
        that.setData({
          userId:res.data.userId
        })
        that.createOrderfn()
      },
      fail: function(res){
      }
    });
    
  },
  createOrderfn(){
    var param={
      productId:this.data.productId,
      userId:this.data.userId,
      rentTypeStr:this.data.subarr.join(","),
      buyCount:this.data.buyCount,
			rentId:this.data.rentConfigdata
    }
	 console.log(param)
      var that=this
       my.request({
        url: requsts.url+"/order/createOrder",
        method: 'POST',
        headers: {'content-type': 'application/x-www-form-urlencoded'},
        data:param,
        dataType: 'json',
        success: function(res) {
          console.log(res)
          if(res.data.code!=0){
              util.toastfn(res.data.msg)
              return
          }
          my.navigateTo({
            url:'/page/common/supplementaryinformation/supplementaryinformation?orderId='+res.data.data,
          })

        },
        fail: function(res) {
            util.toastfn(res.errorMessage)
        },
      });
  },
  onGetAuthorize() {
    if(!this.data.isBtns){
        return
    }
      var that=this
        my.getOpenUserInfo({
          fail: (res) => {
               this.setData({
                showBottom: false,
              });
          },
          success: (res) => {
            let userInfo = JSON.parse(res.response).response // 以下方的报文格式解析两层 response
            console.log(userInfo)
            if(!userInfo.avatar){
              userInfo.avatar=null
            }
            if(!userInfo.nickName){
              userInfo.nickName=null
            }
            var param={
              authCode:that.data.authCode,
              source:"alipay_wallet&avatar",
              avatar:userInfo.avatar,
              nickName:userInfo.nickName,
              gender:userInfo.gender,
              countryCode:userInfo.countryCode,
              province:userInfo.province,
              city:userInfo.city,
            }
            console.log(param)
            that.loginApi(param)
          }
        });
  },
  loginApi(param){
      var that=this
      my.request({
        url: requsts.url+"/user/login",
        method: 'POST',
        data:param,
        headers: {'content-type': 'application/x-www-form-urlencoded'},
        dataType: 'json',
        success: function(res) {
          if(res.data.code!=0){
              util.toastfn(res.data.msg)
          }
          console.log(res)
          that.setData({
              nickNames:res.data.data.nickName,
              avatarUrl:res.data.data.avatar,
            })
          my.setStorage({
            key: 'userId',
            data:{
              userId:res.data.data.userId,
              nickNames:res.data.data.nickName,
              avatarUrl:res.data.data.avatar,
            },
            success: function() {
              that.userIdfnjj()
            }
          });
          util.toastfn("登录成功")
        },
        fail: function(res) {
        },
      });
  },
  userIdfnjj(){
    var that=this
      my.getStorage({
      key: 'userId',
      success: function(res) {
        console.log(res)
         that.setData({
              userId:res.data.userId
          })
          that.createOrderfn()
      },
      fail: function(res){
      }
    });
  },
  //购买数量
  callBackFn(value){
   console.log(value);
   this.setData({
     buyCount:value
   })
  },
});